package com.idea.common.utils;

import java.math.BigDecimal;

/**
 * NumberUtils.java
 * 
 * @author King<br/>
 *         email:my249645546@163.com
 * @Description 数字工具类
 * @since 1.0.0
 * @Date 2012-12-17下午3:30:48
 */
public class NumberUtils {
	/**
	 * 
	 * @Description:为空
	 * @since 1.0.0
	 * @Date:2012-12-27 下午3:25:02
	 * @param number
	 * @return boolean
	 */
	public static boolean isNull(Number number) {
		return number == null;
	}

	/**
	 * 
	 * @Description:不为空
	 * @since 1.0.0
	 * @Date:2012-12-27 下午3:25:15
	 * @param number
	 * @return boolean
	 */
	public static boolean isNotNull(Number number) {
		return number != null;
	}

	/**
	 * 
	 * @Description:不是一个合法的id
	 * @since 1.0.0
	 * @Date:2012-12-17 下午3:34:27
	 * @param id
	 * @return boolean
	 */
	public static boolean isNotAId(Number id) {
		return isNull(id) || id.intValue() <= 0;
	}

	/**
	 * 
	 * @Description:是一个合法的id
	 * @since 1.0.0
	 * @Date:2012-12-17 下午3:34:27
	 * @param id
	 * @return boolean
	 */
	public static boolean isAId(Number id) {
		return isNotNull(id) && id.intValue() > 0;
	}

	/**
	 * 
	 * @Description:大于0
	 * @since 1.0.0
	 * @Date:2012-12-27 下午4:34:15
	 * @param number
	 * @return boolean
	 */
	public static boolean isMoreThanZero(Number number) {
		return isNotNull(number) && number.intValue() > 0;
	}

	/**
	 * 
	 * @Description:是否大于指定数
	 * @since 1.0.0
	 * @Date:2012-12-29 下午4:00:41
	 * @param number
	 * @param compareNum
	 * @return boolean
	 */
	public static boolean isMoreThan(Number number, Number compareNum) {
		return number.intValue() > compareNum.intValue();
	}

	/**
	 * 
	 * @Description:是否大于等于指定数
	 * @since 1.0.0
	 * @Date:2012-12-29 下午4:00:57
	 * @param number
	 * @param compareNum
	 * @return boolean
	 */
	public static boolean isMoreThanOrEql(Number number, Number compareNum) {
		return number.intValue() >= compareNum.intValue();
	}

	/**
	 * 
	 * @Description:四舍五入 保留两位小数
	 * @since 1.0.0
	 * @Date:2013-1-23 下午1:07:26
	 * @param decimal
	 * @return Double
	 */
	public static Double round(Double decimal) {
		return round(decimal, 2);
	}
	
	/**
	 * 
	 * @Description:四舍五入 保留指定位小数
	 * @since 1.0.0
	 * @Date:2013-3-8 下午3:40:21
	 * @param decimal
	 * @param scale
	 * @return
	 * Double
	 */
	public static Double round(Double decimal,Integer scale) {
		BigDecimal bigDecimal = new BigDecimal(decimal);
		Double roundDecimal = bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP)
				.doubleValue();
		return roundDecimal;
	}

	/**
	 * 
	 * @Description:计算两个整数的 商（商为小数）
	 * @since 1.0.0
	 * @Date:2013-1-23 下午1:07:59
	 * @param dividend
	 * @param divisor
	 * @return Double
	 */
	public static Double calculateDoubleQuotientOfTowIntegerNumber(
			Integer dividend, Integer divisor) {
		Double doubleDividend = (double) dividend;
		Double doubleDivisor = (double) divisor;

		Double quotient = doubleDividend / doubleDivisor;
		return quotient;
	}

	/**
	 * 
	 * @Description:计算两个整数的 商（商保留两位小数）
	 * @since 1.0.0
	 * @Date:2013-1-23 下午1:10:54
	 * @param dividend
	 * @param divisor
	 * @return Double
	 */
	public static Double calculateRoundDoubleQuotientOfTowIntegerNumber(
			Integer dividend, Integer divisor) {
		return round(calculateDoubleQuotientOfTowIntegerNumber(dividend,
				divisor));
	}
	
	/**
	 * 
	 * @Description:返回一个有效的id值
	 * @since 1.0.0
	 * @Date:2013-3-6 上午11:10:06
	 * @param id
	 * @return
	 * Number
	 */
	@SuppressWarnings("unchecked")
	public static <T extends Number> T toValidId(T id){
		if(isNotAId(id)){
			Number num=0;
			return (T) num;
		}
		return id;
	}
}
